package GUILayer;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import CtrLayer.StaffCtr;


public class Main extends JFrame{
	private static final long serialVersionUID = 1L;
	private boolean logged = false;
	protected static boolean access = false;
	protected static String logUS = "";
	
	private JPanel mainFrame;
	private StaffCtr staffCtr;
	private ProductGUI productGUI;
	private CustomerGUI customerGUI;
	private ContractorGUI contractorGUI;
    private OrdersAndLeasingGUI orderGUI;
    private DiscountGUI discountGUI;
    private StaffGUI staffGUI;
    
	public Main()
	{
		super("UCN dmai0913 GROUP 2 - 1sem Project");
		staffCtr = new StaffCtr();
		productGUI = new ProductGUI();
		customerGUI = new CustomerGUI();
		contractorGUI = new ContractorGUI();
		orderGUI = new OrdersAndLeasingGUI();
		discountGUI = new DiscountGUI();
		staffGUI = new StaffGUI();
		
		setSize(400, 200);
		setResizable(false);
		
		
		mainFrame = new JPanel();
		mainFrame.setLayout(new GridLayout(1,1));
		
		setContentPane(mainFrame);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		
		logIN();  //<-- skip login to focus on tabs for now   logUS="1"; makeFrame();
		
		setLocationRelativeTo(null);
	}
	

	
	
	
	private void logIN()
	{
		if(logged==false)
		{
			final JPanel login = new JPanel();
			GridBagLayout g = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();
			
			login.setLayout(g);
			
			JLabel userLable = new JLabel("User");
			final JTextField userText = new JTextField(10);
			JLabel passwordLable = new JLabel("Password");
			final JPasswordField passwordField = new JPasswordField(10);
			JButton loginButton = new JButton("Log In");
			
			userLable.setBounds(10, 10, 80, 25);
			userText.setBounds(10,40,80,25);
			passwordLable.setBounds(10, 10, 80, 25);
			passwordField.setBounds(10,40,80,25);
			loginButton.setBounds(10, 80, 80, 25);
			
			JPanel upTEXT = new JPanel();
			upTEXT.setLayout(new GridLayout(0,1));
			upTEXT.add(userLable);
			upTEXT.add(passwordLable);
			
			JPanel upFIELD = new JPanel();
			upFIELD.setLayout(new GridLayout(0,1));
			upFIELD.add(userText);
			upFIELD.add(passwordField);
			
			JPanel loginP = new JPanel();
			GridLayout grid = new GridLayout(0,2);
			grid.setVgap(10);
			loginP.setLayout(grid);
			loginP.add(upTEXT);
			loginP.add(upFIELD);
			loginP.add(new JLabel());
			loginP.add(loginButton);
			
			c.gridx = 0;
			c.gridy = 0;
			login.add(loginP,c);


			mainFrame.add(login);
			setVisible(true);
			
			
			loginButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String user = userText.getText();
					String pass = "";
					char[] pw = passwordField.getPassword();
					
					for(int i=0;i<pw.length;i++) pass += pw[i];
					
					int res = staffCtr.login(user, pass);
					
					if(res!=0)
					{
						logged = true;
						logUS = user;
						
						if(res==2) access=true;
						JOptionPane.showMessageDialog(login,
							    "You have successfully logged in !",
							    "Welcome "+user,
							    JOptionPane.PLAIN_MESSAGE);
						
						clearFrame();
						makeFrame();
					}
					else
					{
						JOptionPane.showMessageDialog(login,
							    "Username or Password is incorrect",
							    "Login error",
							    JOptionPane.ERROR_MESSAGE);
						
						userText.selectAll();
						userText.requestFocus();
					}
				}
			});
		}
	}
	
	
	
	private void clearFrame()
	{
		mainFrame.removeAll();
	}
	
	
	private void makeFrame()
	{
		clearFrame();
		setSize(700, 550);
		
		JTabbedPane tabs = new JTabbedPane();
		
		JComponent panel1 = orderGUI.makeProductsTab();
		JComponent panel2 = productGUI.makeProductsTab();
		JComponent panel3 = discountGUI.makeDiscountTab();
		JComponent panel4 = customerGUI.makeCustomerTab();
		JComponent panel5 = contractorGUI.makeContractorTab();
		JComponent panel6 = staffGUI.makeStaffTab();
		
		tabs.addTab("Orders and Leasing", panel1);
		tabs.addTab("Products", panel2);
		if(access==true) tabs.addTab("Discounts", panel3);
		tabs.addTab("Customers", panel4);
		tabs.addTab("Contractors", panel5);
		if(access==true) tabs.addTab("Staff", panel6);
		
		
		mainFrame.add(tabs);
		setVisible(true);
	}
	
	
	
	
	
	public static void main(String[] args)
	{
		new Main();
	}
	
}
